package com.educate.ucenter.rpc.provide.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.educate.common.utils.converter.ListUtils;
import com.educate.common.utils.exception.AppException;
import com.educate.common.web.BeanUtils;
import com.educate.ucenter.dao.ChildDao;
import com.educate.ucenter.dao.FamilyDao;
import com.educate.ucenter.model.bo.ChildBO;
import com.educate.ucenter.model.bo.FamilyBO;
import com.educate.ucenter.model.po.Child;
import com.educate.ucenter.rpc.api.ChildService;
import com.educate.ucenter.rpc.api.FamilyService;
import lombok.extern.log4j.Log4j2;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Page;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Log4j2
@Service(
        version = "${api.service.version}",
        application = "${dubbo.application.id}",
        protocol = "${dubbo.protocol.id}",
        registry = "${dubbo.registry.id}"
)
public class ChildServiceImpl implements ChildService {

    @Resource ChildDao childDao;
    @Resource FamilyDao familyDao;
    @Resource FamilyService familyService;

    @Override
    public ChildBO create(ChildBO bo) {
        bo.setChildId(new ObjectId().toHexString());
        FamilyBO defFamily = familyService.getOrCreateDefFamily(bo.getUid());
        defFamily.getChildIds().add(bo.getChildId());
        if (defFamily.getChildId() == null) {
            defFamily.setChildId(bo.getChildId());
        }
        childDao.insert(bo);
        familyService.update(defFamily);
        return bo;
    }

    @Override
    public void delete(ChildBO bo) {
        childDao.delete(bo);
    }

    @Override
    public void delete(String childId) {
        childDao.deleteById(childId);
    }

    @Override
    public ChildBO detail(String childId) {
        return childDao.findById(childId)
                .map(ChildBO::new)
                .orElse(null);
    }

    @Override
    public ChildBO detail(ChildBO bo) {
        return childDao.findById(bo.getChildId())
                .map(ChildBO::new)
                .orElse(null);
    }

    @Override
    public void update(ChildBO bo) {
        Optional<Child> optional = childDao.findById(bo.getChildId());
        if (!optional.isPresent()) {
            throw new AppException("不存在的实例");
        }
        BeanUtils.copyPropertiesNonEmpty(bo, optional.get());
        childDao.save(optional.get());
    }

    @Override
    public Page<ChildBO> query(ChildBO bo) {
        return childDao.query(bo, bo.pageable()).map(ChildBO::new);
    }

    @Override
    public List<ChildBO> list(ChildBO bo) {
        List<Child> result = childDao.list(bo, bo.pageable());
        return ListUtils.map(result, ChildBO::new);
    }

    @Override
    public List<ChildBO> listByUser(ChildBO bo) {
        return null;
    }

    @Override
    public List<ChildBO> listByFamily(ChildBO bo) {
        return familyDao.findById(bo.getFamilyId())
                .map(family -> family.getChildIds()
                        .stream()
                        .map(childId -> childDao.findById(childId)
                                .map(ChildBO::new)
                                .orElse(null))
                        .filter(Objects::nonNull)
                        .collect(Collectors.toList()))
                .orElse(Collections.emptyList());
    }
}
